package com.it.gw.modules.station.adapter

import GWRunningStaticVo
import StationTypeDtos
import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.it.common.utils.DisplayUtil
import com.it.common.utils.RvSpacesItemUtils
import com.it.common.utils.StringUtils
import com.it.common.utils.isNotEmpty
import com.it.data.manager.DataManager
import com.it.data.network.rsp.WrongRecVo
import com.it.gw.R
import com.it.gw.databinding.ItemEqStaticBinding
import com.it.gw.databinding.ItemGwRunningBinding
import java.text.DecimalFormat


class GWRunningAdapter(
    var context: Context,
    var listener: (GWRunningStaticVo) -> Unit,
    var datas: List<GWRunningStaticVo>
) : RecyclerView.Adapter<GWRunningAdapter.ViewHolder>() {
    companion object {
        const val TAG = "GWRunningAdapter"
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): ViewHolder {
        val binding =
            ItemGwRunningBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return ViewHolder(binding)
    }


    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val data = datas[position]
        Log.d(TAG, "onBindViewHolder: $data")
        holder.binding.name.setText(data.trnm?.map { it.toString() }?.joinToString("\n"))
        holder.binding.root.setOnClickListener {
            listener.invoke(data)
        }


        val map = hashMapOf(RvSpacesItemUtils.BOTTOM_DECORATION to 1)
        holder.binding.rvList.addItemDecoration(RvSpacesItemUtils(map))




        if (data.stationTypeDtos.isNotEmpty()) {
            val existingTotal = data.stationTypeDtos!!.sumOf { it.existingTotal.toDouble() }
            val normalSite = data.stationTypeDtos!!.sumOf { it.normalSite.toDouble() }
            val arrivalReport = data.stationTypeDtos!!.sumOf { it.arrivalReport.toDouble() }
            val dto = StationTypeDtos(
                sttp = "合计",
                existingTotal = DataManager.formatDouble(existingTotal),
                normalSite = DataManager.formatDouble(normalSite),
                arrivalReport = DataManager.formatDouble(arrivalReport),
            )
            data.stationTypeDtos!!.add(0, dto)
            holder.binding.rvList.adapter =
                GWRunningDataAdapter(context = context, datas = data.stationTypeDtos!!, listener = {

                })
        }


    }


    override fun getItemCount() = datas.size

    class ViewHolder(val binding: ItemGwRunningBinding) : RecyclerView.ViewHolder(binding.root)
}